﻿//callbackFn
var _store_MyStorckFn = function(options,success,response){
    Ext.MessageBox.alert('信息',response.responseText);
    _store_my.load({params: {start: 0, limit: 25}});
}

//行情store配置
var _store_config = function(codePre){
    return {
       url: '/WebHandler/stockHandler.ashx',
       baseParams:{codePre:codePre,action:'stockinfo'},
       lastOptions: {params: {start: 0, limit: 25}},
       autoLoad: {params: {start: 0, limit: 25}},
       root:'rows',
       totalProperty:'count',
       fields:['name','code','open','close','price','max','min','cj','cjj','time']
    }
}
//上海与深圳两地股市数据
var _store_sz = new Ext.data.JsonStore(_store_config('sz'));
var _store_sh = new Ext.data.JsonStore(_store_config('sh'));
var _store_my = new Ext.data.JsonStore(_store_config('my'));//自选股
var _store_grid_color_rendererFn = function(value, metaData, record, rowIndex, colIndex, store) {
      if(record.data['price']>record.data['close'])
        return '<span style="color:red;">' + value + '</span>';
      else if(record.data['price']<record.data['close'])
        return '<span style="color:green;">' + value + '</span>';
      else
        return value;
   }

//表格列设定
var _stock_cm = new Ext.grid.ColumnModel({
    columns: [
        new Ext.grid.RowNumberer(),
        { header: "股票代码", dataIndex:'code'},
        { header: "股票名字", dataIndex:'name'},
        { header: "今开盘价", dataIndex:'open'},
        { header: "昨收盘价", dataIndex:'close'},
        { header: "当前价格", dataIndex:'price'},
        { header: "最高价", dataIndex:'max'},
        { header: "最低价", dataIndex:'min'},
        { header: "成交数", dataIndex:'cj'},
        { header: "成交金额", dataIndex:'cjj'},
        { header: "时间", dataIndex:'time',width:130}
    ],
    defaults: {
        sortable: true,width:60,renderer:_store_grid_color_rendererFn
    }
});


//应用程序
MyDesktop.StockMarketWindow = Ext.extend(Ext.app.Module, {
    id:'stock-market-win',
    init : function(){
        this.launcher = {
            text: '股票行情',
            iconCls:'icon-market',
            handler : this.createWindow,
            scope: this
        }
    },
    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('stock-market-win');
        if(!win){
            win = desktop.createWindow({
                id: 'stock-market-win',
                title:'股票行情',
                width:780,
                height:480,
                iconCls: 'icon-market',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                border:false,
                layout: 'fit',
                items:new Ext.TabPanel({
                    activeTab: 0,
                    plain:true,
                    border:false,
                    defaults:{
                        autoScroll: true,
                        xtype:'grid',
                        border:false,
                        frame: true,
                        sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
                        cm:_stock_cm,
                        viewConfig: {
                            forceFit: true
                        }
                    },
                    items:[{
                            title:'深圳',
                            store:_store_sz,
                            bbar: new Ext.PagingToolbar({
                                pageSize: 25,
                                store: _store_sz,
                                displayInfo: true,
                                displayMsg: '显示数据 {0} - {1} of {2}',
                                emptyMsg: "无数据"
                            })
                        },{
                            title:'上海',
                            store:_store_sh,
                            bbar: new Ext.PagingToolbar({
                                pageSize: 25,
                                store: _store_sh,
                                displayInfo: true,
                                displayMsg: '显示数据 {0} - {1} of {2}',
                                emptyMsg: "无数据"
                            })
                        },{
                            id:'my_stock_grid',
                            title:'自选股',
                            store:_store_my,
                            sm: new Ext.grid.RowSelectionModel({singleSelect:false}),
                            bbar: new Ext.PagingToolbar({
                                pageSize: 25,
                                store: _store_my,
                                displayInfo: true,
                                displayMsg: '显示数据 {0} - {1} of {2}',
                                emptyMsg: "无数据"
                            }),
                            tbar:[{
                                    text:'添加新纪录',
                                    tooltip:'添加一条新的买卖纪录',
                                    iconCls:'add',
                                    handler: function(){
                                        Ext.MessageBox.prompt('添加', '请输入股票代码:', function(btn,text){
   if(btn == 'ok'){
        Ext.Ajax.request({
           url: '/WebHandler/stockHandler.ashx',
           callback :_store_MyStorckFn,
           params: { action: 'addMyStockInfo',code:text }
        });
  
   }
});
                                    }
                                },'-',{
                                    text:'删除纪录',
                                    tooltip:'删除选中的纪录',
                                    iconCls:'remove',
                                    handler: function(){
                                        Ext.MessageBox.show({
                                           title: '删除确认',
                                           msg: '确定要移除嘛!',
                                           buttons: Ext.MessageBox.OKCANCEL,
                                           icon: Ext.MessageBox.WARNING,
                                           fn:function(btn){
                                                if(btn=='ok'){
                                                    var records = Ext.getCmp('my_stock_grid').getSelectionModel().getSelections();
                                                    var code = [];
                                                    Ext.each(records,function(record){
                                                        code.push('\''+record.data['code']+'\'');
                                                    })
                                                    Ext.Ajax.request({
                                                       url: '/WebHandler/stockHandler.ashx',
                                                       callback :_store_MyStorckFn,
                                                       params: { action: 'removeMyStockInfo',code:code.join(',') }
                                                    });
                                                }
                                           }
                                       });

                                    }
                                }]
                        }]
                })
            });
        }
        win.show();
    }
});

//将应用程序添加入桌面
MyDesktop._modules.push(new MyDesktop.StockMarketWindow());